﻿jQuery.fn.verticalAlign = function (height) {
    return this.css("top", ($(window).height() - height) / 2 + 'px')
};

function AjaxLoading(loading) {
    if (loading)
        $("body").addClass("loading");
    else
        $("body").removeClass("loading");
}

function GetQueryStringHash(name) {
    var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.hash);
    return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
}

function GetQueryStringHref(name) {
    var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.href);
    return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
}


function CallAjaxAspx(methodtype, methodname, url, data, funsuccess, funbeforeSend, funcomplete) {
    debugger;
    $.ajax({
        async: false,
        type: methodtype,
        url: url + "/" + methodname,
        data: JSON.stringify(data),
        contentType: "application/json",
        dataType: "text",
        success: function (data) {
            debugger;
            var obj = JSON.parse((JSON.parse(data).d));
            if (typeof obj.Cause != "undefined")
                alertbox(obj.Cause, function () { });
            funsuccess(JSON.parse(data).d);
            funcomplete();
        },
        beforeSend: funbeforeSend,
    });
}

function CallAjaxAshx(methodtype, methodname, url, data, funsuccess, funbeforeSend, funcomplete) {
    var ajaxarg = {
        method: methodname,
        args: data
    };
    $.ajax({
        async: false,
        type: methodtype,
        url: url,
        data: ajaxarg,
        success: funsuccess,
        beforeSend: funbeforeSend,
        complete: funcomplete
    });
}


function confirm(title, message, titlebuttonOK, titlebuttonCancel, callback) {
    var confirmModal = $('<div id="dialog_simple">');
    confirmModal.attr('title', title)
    confirmModal.append($('<p>').append(message));

    confirmModal.dialog({
        width: 500,
        autoOpen: false,
        resizable: false,
        modal: true,
        buttons: [{
            html: titlebuttonOK,
            width: 100,
            "class": "btn btn-success",
            click: function () {
                callback();
                $(this).dialog("close");
            }
        }, {
            html: titlebuttonCancel,
            width: 100,
            "class": "btn btn-default",
            click: function () {
                $(this).dialog("close");
            }
        }]
    });

    confirmModal.dialog('open');

}

function alertbox(message, closefunction) {
    debugger;
    var confirmModal = $('<div id="dialog_simple">');
    confirmModal.attr('title', "alert")
    confirmModal.append($('<p>').append(message));

    confirmModal.dialog({
        autoOpen: false,
        resizable: false,
        modal: true,
        buttons: [{
            html: "Close",
            width: 100,
            "class": "btn btn-success",
            click: function () {
                $(this).dialog("close");
                closefunction();
            }
        }]
    });

    confirmModal.dialog('open');
}


//function alertSmallBox(message, time, color) {
//    $.smallBox({
//        title: message,
//        content: "<i class='fa fa-clock-o'></i> <i>" + time + "</i>",
//        color: color,
//        iconSmall: "fa fa-thumbs-up bounce animated",
//        timeout: 4000
//    });
//}

function alertSmallBox(title, message, type) {
    var icon = "fa-thumbs-up";
    var color = "#8ac38b";
    switch (type) {
        case "error":
            icon = "fa-times";
            color = "#c26565";
            break;
        case "info":
            color = "#9cb4c5";
            icon = "fa-info-circle";
            break;
    }
    $.smallBox({
        title: title,
        content: message,
        color: color,
        iconSmall: "fa " + icon + " bounce animated",
        timeout: 4000
    });
}


//Script phân trang


function CreateLinkPage(text, page, isCurrentPage, disabled, funclick) {
    var li = $('<li>');
    var a = $('<a href="javascript:void(0);">').append(text);
    li.append(a);
    a.attr('gotopage', page);
    if (!isCurrentPage) {
        a.click(function () {
            debugger;
            funclick(parseInt($(this).attr('gotopage')));
        });
    }
    else {
        li.addClass("active");
    }
    if (disabled)
        li.addClass("disabled");
    return li;
}

function CreatePaging(divpaging, curentpage, totalpage, funclick) {
    debugger;
    divpaging.empty();
    var ul = $('<ul class="pagination">');
    divpaging.append(ul);
    var i;
    ul.append(CreateLinkPage($('<i class="fa fa-arrow-left">'), 1, false, curentpage <= 1, funclick));
    for (i = curentpage - 3; i <= curentpage + 3; i++) {
        if (i > totalpage) break;
        if (i >= 1) {
            ul.append(CreateLinkPage(i, i, i == curentpage, false, funclick));
        }
    }
    ul.append(CreateLinkPage('<i class="fa fa-arrow-right"></i>', totalpage, false, curentpage >= totalpage, funclick));
}

function LoadDataWithPaging(targetdata, targetpaing, page, typemethod, url, dataarg, funshowdata, numberinpage, funclick) {
    debugger;
    $.ajax({
        type: typemethod,
        url: url,
        data: dataarg,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        beforeSend: AjaxLoading(true),
        success: function (response) {
            debugger;
            AjaxLoading(false);
            var obj = JSON.parse(response.d);
            if (obj.Status) {
                var _totalpage = Math.round(obj.TotalItem / numberinpage);
                var totalpage = ((obj.TotalItem / numberinpage) > _totalpage) ? _totalpage + 1 : _totalpage;
                funshowdata(obj.TotalItem, targetdata, obj.Data);
                CreatePaging(targetpaing, page, totalpage, funclick);
            }
            else {
                var Cause = obj.Cause;
                if (typeof Cause != "undefined")
                    alertbox(Cause, function () { });
            }

        },
        failure: function (response) {
            alertbox("Lỗi, Vui lòng thử lại sau", function () { });
        }
    });
}

// script create button
function CreateButton(dataid, cssclass) {
    var i = $('<i class="fa btn btn-xs btn-default" style="margin-right:5px;">');
    i.addClass(cssclass);
    i.attr('dataid', dataid);
    return i;
}


var Common = {
    CreateCheckBox: function (id) {
        var label = $('<label class="checkbox" style="margin-top:0px;">');
        var input = $('<input type="checkbox" name="checkbox">');
        input.attr('dataid', id);
        input.attr('checktype', "selectarticle");
        label.append(input);
        var i = $('<i>');
        label.append(i);
        return label;
    },
    CreateCell: function (text, id) {
        var td = $('<td>').attr("dataid", id).text(text);
        return td;
    }

}